package com.msb.mall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.msb.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.stereotype.Component;

@Component
@Data
public class AlipayTemplate {
    // 商户appid 沙箱账号: tklalf8880@sandbox.com
    public static String APPID = "2021000122627124";
    // 私钥 pkcs8格式的
    public static String RSA_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC6s8UiCjDDVEntSpKGJkUneQzzwk/iWhaeYkcle3Ru3DIcK5QdSIBudPyMbEM717J+twcHYYUp3R5k1AeiOnjS9mjDzdVXO+ahCD8e2DGznGnFlASEHWQ7J0lGjBISb1+NGQA4IWu2HYobAUhmkQ287WKS/TTDiXmKQ5BjEtb776LCrUGibKFHc1L/zYuO9BUvthwcdySlX0nPH1DmR1h7qFLlszP0q3GyCVaKAx9dwPvlegjS6luaSpxGOwYky7Br0IzYDs9+pkSI2tK7vExM8jc4Id3vZ73SE3xxT9zSx6H+5F5958aSYwapHCRr8F+bPyhX3HLzIaZyD9EluzmJAgMBAAECggEAF4OYqmbaKV+uR+KCfb7IKuaGCRVJ9loeSyhZiZD44xV8givC5cVB16eIPy9LcHgEFQ18JyPTH6insU86/uWaXgeGvlJoMiprK5DcUkgsSlDCyY2Q3ArPmtuh0O7HP+cjA6RIcMlUxYUG5cvNkI1oS4kiRxz4zj+8PPuxbajrxqL6rCPeD5/FRQ8Gl78IxkShlqgU7c2uTin2quAo2vz4p0qhOHnXoPzRnZOnA94ovnjtzjwKC+VeSA8wCqOQCLU3y+eTYose9VinhUZd88BJwqU5fbkGT8nRR0fQL7hAwzXQkPi2NwyAuVBxkcTbuLCc/ro4yhot3sw5bF8B3ywe+QKBgQDdHEQAFr8tvuht8E9jv0Js8Au7UGOezJNoIZkwI50+SJkgpkxOfgNbvyjx8KEsb0iFES9EBWppHijC9zUcUoEWYP/r8nrEhM0OY/uNUd8F9fPfgTAYAYSLw2HJ0UAEMKNVREmOQ3U1OEsvxt3O0Ve/BRQXaR/hpZDoeSb69FvegwKBgQDYKZbHu8GlQZIKAbvMNr7AB0o+X7CkjombbujTrtHv4vAqRgGY5F/s7y1qoYjC1plU7KzErkEqs8pCNSssP20jGEuIc/wVYfKd/D7iWNxMJEi2h6lCJrQUzpUuJHvDrwa31M3T5UHhgESJNwalk0Qmx3Hbo2bHvEl2Tv6aFGeKAwKBgQDERNjfs1FUjAfJvuzZv+Z4yMPBK+SiCzo1zs0p/BMSPGMgpj1J13WntRHlYRHQXZ/j7pMkMCJ62LiaB9HvKBVKBiRmDsGHO9LBmWKr2Sa9JcuqdAR66q37Wh2thb7reTki8RDR5XNfIG8tI7u0YAMhnqJs/XW9WLg+g3xkexApfQKBgHdfgEUNCwOYEm40RuGf2fdyDy4i6gn2LUgJF8rO4GD5kmfGAVvNwqu16f7Y9g2jNUoQ6dgz4wkSAMBPXGgzHrIi2JOiebPVQ4a7pn6ZVA84em9Su8YsWYakDv5MZ4zJO4yJmKFMGfXOQAsGuET37sr7mYUR19C1ylvrg+7TFdrBAoGBANDxsuqqKkIPD96Bepi/QpLluzThfRZoA5cKde+8e5IpRtclszqWqPY1GemkWqu+N4p2d7T8XFDGs9Rhodt/PsawEKDLz7dXwDe2rZt/OxSwa/p8GsFiQqxY9PM9AFgoZa3EsmnibXZMXgYocu9zAGG2NPXGWfj+lgfZyg4b+6kJ";
    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url = "http://order.msb.com/payed/notify";
    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
    public static String return_url = "http://order.msb.com/orderPay/returnUrl";
    // 请求网关地址
    public static String URL = "https://openapi.alipaydev.com/gateway.do";
    // 编码
    public static String CHARSET = "UTF-8";
    // 返回格式
    public static String FORMAT = "json";
    // 支付宝公钥
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAurPFIgoww1RJ7UqShiZFJ3kM88JP4loWnmJHJXt0btwyHCuUHUiAbnT8jGxDO9eyfrcHB2GFKd0eZNQHojp40vZow83VVzvmoQg/Htgxs5xpxZQEhB1kOydJRowSEm9fjRkAOCFrth2KGwFIZpENvO1ikv00w4l5ikOQYxLW+++iwq1BomyhR3NS/82LjvQVL7YcHHckpV9Jzx9Q5kdYe6hS5bMz9KtxsglWigMfXcD75XoI0upbmkqcRjsGJMuwa9CM2A7PfqZEiNrSu7xMTPI3OCHd72e90hN8cU/c0seh/uRefefGkmMGqRwka/Bfmz8oV9xy8yGmcg/RJbs5iQIDAQAB";
    // 日志记录目录
    public static String log_path = "/log";
    // RSA2
    public static String SIGNTYPE = "RSA2";

    public String pay(PayVo payVo){
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(URL,
                APPID,
                RSA_PRIVATE_KEY,
                FORMAT,
                CHARSET,
                ALIPAY_PUBLIC_KEY,
                SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(payVo.getOut_trader_no());
        model.setSubject(payVo.getSubject());
        model.setTotalAmount(payVo.getTotal_amount());
        model.setBody(payVo.getBody());
        model.setTimeoutExpress("5000");
        model.setProductCode("11111");
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(return_url);

        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            return form;
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  null;
    }
}
